<template>
  <el-container class="home">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" width="60px" />
        <label>电商后台管理系统</label>
      </div>
      <el-button type="info" @click="quitLogin">退出登录</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 导航菜单 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 切换 -->
        <div class="toggle-menu" @click="menuCollapse">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#3BB8EF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activeIndex"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              class="el-icon-menu"
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="activeNavMenu('/'+subItem.path)"
            >
              <template slot="title">{{ subItem.authName }}</template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主要内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  created() {
    this.getMenuList()
    this.activeIndex=window.sessionStorage.getItem('index')
  },
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-unlock',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-claim',
        145: 'el-icon-data-line',
      },
      isCollapse: false,
      activeIndex:'',
    }
  },
  methods: {
    // 退出登录
    quitLogin() {
      window.sessionStorage.clear()
      this.$message.success('退出成功')
      this.$router.push('/login')
    },
    // 获取菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 菜单展开按钮
    menuCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 激活菜单显示
    activeNavMenu(activeIndex){
      window.sessionStorage.setItem('index',activeIndex)
      this.activeIndex=activeIndex
    }
  },
}
</script>

<style lang="less" scoped>
.home {
  height: 100%;
}

.el-header {
  background-color: gray;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;

  div {
    display: flex;
    align-items: center;

    label {
      padding-left: 10px;
    }
  }
}

.el-aside {
  background-color: #545c64;
}

.el-menu {
  border-right: none;
}

.el-icon-menu:before {
  margin-right: 10px;
}

.toggle-menu {
  color: white;
  background-color: rgba(122, 108, 108, 0.644);
  text-align: center;
  height: 30px;
  line-height: 30px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style> 